package com.ifast.integral.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ifast.integral.dao.IntegralUserDao;
import com.ifast.integral.domain.IntegralHistory;
import com.ifast.integral.domain.IntegralUser;
import com.ifast.integral.service.IntegralHistoryService;
import com.ifast.integral.service.IntegralUserService;
import com.luculent.util.extension.api.Result;

import cn.hutool.core.bean.BeanUtil;


/**
 * 
 * @author ShiQiang
 * @date 2019年4月9日09:56:53
 */
@Transactional
@Service("integralUserService")
public class IntegralUserServiceImpl extends ServiceImpl<IntegralUserDao, IntegralUser> implements IntegralUserService{

	@Autowired
    private IntegralHistoryService integralHistoryService;  
	
	@Override
	public Result<?> info(String userId) {
		IntegralUser user = this.selectOne(new EntityWrapper<IntegralUser>(IntegralUser.builder().userId(userId).build()));
		if(user == null){
			return Result.fail_msg("没有积分信息");
		}
		Calendar calendar = Calendar.getInstance(); 
		List<IntegralHistory> histories = integralHistoryService.selectList(new EntityWrapper<IntegralHistory>() 
				.eq("USER_ID",userId)
				.eq("nian", calendar.get(Calendar.YEAR))
				.eq("yue", calendar.get(Calendar.MONTH)+1)
				.eq("ri", calendar.get(Calendar.DAY_OF_MONTH))); 
		user.setTotal(user.getTotal() == null ? 0 : user.getTotal()).setTodayToal(histories.stream().mapToInt(hist -> hist.getScore()).sum());
        return Result.ok(BeanUtil.beanToMap(user));
	}
}